package com.ap.core.jbehave.retrievertp;

import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;

import com.ap.core.cxf.jaxb.managepayment.RTPSummaryType;
import com.ap.core.cxf.jaxb.managepayment.RetrieveRTP1Request;
import com.ap.core.cxf.jaxb.managepayment.RetrieveRTP1Response;
import com.ap.core.cxf.jaxb.managertp.SubmitRTP1Request;
import com.ap.core.cxf.jaxb.managertp.SubmitRTP1Response;
import com.ap.core.cxf.jaxb.managertp.SubmitRTPMessageType;
import com.ap.core.cxf.jaxb.rtp.RTPSenderInfoType;
import com.ap.core.jbehave.BaseTransitionRTPSteps;
import com.ap.core.jbehave.StoryContext;
import com.ap.core.util.JaxbUtil;
import com.ap.core.util.Nullable;
import com.ap.core.util.TypedKey;
import com.core.ap.wsdl.managepaymentservice_1.ManagePayment1PortType;
import com.core.ap.wsdl.managepaymentservice_1.RetrieveRTP1Fault;
import com.core.ap.wsdl.managertpservice_1.ManageRTP1PortType;
import com.core.ap.wsdl.managertpservice_1.SubmitRTP1Fault;

public class RetrieveRTPSteps extends BaseTransitionRTPSteps {

    public static final TypedKey<RTPSummaryType> RTP_SUMMARY = new TypedKey<>("RTP Summary");
    public static final TypedKey<RTPSenderInfoType> RTP_SENDER_INFO = new TypedKey<>("RTP Sender Info");
    public static final TypedKey<String> RTP_MSGTYPE = new TypedKey<>("RTP Message Type");
    private SubmitRTP1Request submitRTPRequest;
    private RetrieveRTP1Request retrieveRTPRequest;
    private SubmitRTP1Response responseRTPType;

    /**
     * @param storyContext
     */
    public RetrieveRTPSteps(StoryContext storyContext) {
        super(storyContext);
    }

    @Given("the OSB has submitted a structurally valid SUBMITRTPFORMOB request to APCore")
    public void givenValidRequestSubmitted() {

    }

    @Given("APCore successfully responded and stored the request with RTPSUBMITTED")
    public void givenAPCoreRespondedWithRTPSUBMITTED() throws SubmitRTP1Fault {
        LOG.info("Given - APCore successfully responded and stored the request with RTPSUBMITTED");
        LOG.info("Given - the OSB has submitted a structurally valid SUBMITRTPFORMOB request to APCore");
        final ManageRTP1PortType singnedPort = factory.getBean(ManageRTP1PortType.class);
        submitRTPRequest = createSubmitRTPRequest(SubmitRTPMessageType.SUBMITRTPFORMOB);
        submitRTPRequest.getRTPSenderInfo().getDistributorInfo().setDistributorId("000001");
        submitRTPRequest.getRTPSenderInfo().getDetailMerchantInfo().getMerchantInfo().setMerchantId("000001");

        LOG.info("When -  a Consumer chooses to make payment using AP service through m-com site to purchase a digital product");
        JaxbUtil.newInstance().javaToXML(SubmitRTP1Request.class, submitRTPRequest);
        responseRTPType = singnedPort.submitRTP1(submitRTPRequest);

        LOG.info("When -  Response Received");
        JaxbUtil.newInstance().javaToXML(SubmitRTP1Response.class, responseRTPType);

    }


    @When("APCore receives the structurally valid RETRIEVERTP")
    public void whenAPCoreReceivedValidRETRIEVERTP() throws RetrieveRTP1Fault {
        LOG.info("When -  the consumer has been authenticated");

        final ManagePayment1PortType singnedPort = factory.getBean(ManagePayment1PortType.class);
        retrieveRTPRequest = createRetrieveRTPRequest();
        retrieveRTPRequest.getRequestInfo().setFIId("000002");
        retrieveRTPRequest.getRequestInfo().setFIConsumerId("hsbccon2");
        retrieveRTPRequest.getRequestInfo().setApTransactionId(responseRTPType.getResponseInfo().getApTransactionId());
        retrieveRTPRequest.getRequestInfo().setApTRId(responseRTPType.getResponseInfo().getApTRId());
        retrieveRTPRequest.getRequestInfo().setBRN(null);
        
        JaxbUtil.newInstance().javaToXML(RetrieveRTP1Request.class, retrieveRTPRequest);
        RetrieveRTP1Response retrieveRTPResponse = singnedPort.retrieveRTP1(retrieveRTPRequest);
        LOG.info("When -  Response Received");
        JaxbUtil.newInstance().javaToXML(RetrieveRTP1Response.class, retrieveRTPResponse);

        if (retrieveRTPResponse != null) {
            if (retrieveRTPResponse.getRTPSummary() != null) {
                storyContext.put(RTP_SUMMARY, retrieveRTPResponse.getRTPSummary());
            }
            if (retrieveRTPResponse.getRTPSenderInfo() != null) {
                storyContext.put(RTP_SENDER_INFO, retrieveRTPResponse.getRTPSenderInfo());
            }
            if (retrieveRTPResponse.getMsgHdr() != null) {
                storyContext.put(RTP_MSGTYPE, retrieveRTPResponse.getMsgHdr().getMsgType().toString());
            }
        }

    }

    @Then("the BANKID in the request will be validated against the BANKID reference data")
    public void thenBankIdValidated() {

    }

    @Then("the COSUMERID in the request will be validated against the CONSUMERID reference data")
    public void thenCOSUMERIDValidated() {

    }

    @Then("the request has the APTransactionID")
    public void thenReqHasApTransactionId() {

    }

    @Then("the expiry timestamp of the payment order record related to te APTransactionID is within a valid expiry time")
    public void thenExpiryTimeStampValidated() {

    }

    @Then("the payment order record should be updated with the status RETRIEVED")
    public void thenPaymentOrderUpadtedRETRIEVED() {

    }

    @Then("the RTPFORAUTH message is sent to the OSB")
    public void thenRTPFORAUTHtoOSB() {

        doRetrieveRTPResponseAssertions(Nullable.nullable(storyContext.get(RTP_SUMMARY)), Nullable.nullable(storyContext.get(RTP_SENDER_INFO)), Nullable.nullable(storyContext.get(RTP_MSGTYPE)));

    }


    @Given("the service consumer has submitted a structurally valid retrievRTP request to APCore")
    public void givenValidecomRequestSubmitted() throws SubmitRTP1Fault {
        LOG.info("Given - APCore successfully responded and stored the request with RTPSUBMITTED");
        LOG.info("Given - the OSB has submitted a structurally valid SUBMITRTPFORBRN request to APCore");
        final ManageRTP1PortType singnedPort = factory.getBean(ManageRTP1PortType.class);
        submitRTPRequest = createSubmitRTPRequest(SubmitRTPMessageType.SUBMITRTPFORBRN);
        submitRTPRequest.getRTPSenderInfo().getDistributorInfo().setDistributorId("000001");
        submitRTPRequest.getRTPSenderInfo().getDetailMerchantInfo().getMerchantInfo().setMerchantId("000001");

        LOG.info("When -  a Consumer chooses to make payment using AP service through m-com site to purchase a digital product");
        JaxbUtil.newInstance().javaToXML(SubmitRTP1Request.class, submitRTPRequest);
        responseRTPType = singnedPort.submitRTP1(submitRTPRequest);

        LOG.info("When -  Response Received");
        JaxbUtil.newInstance().javaToXML(SubmitRTP1Response.class, responseRTPType);

        retrieveRTPRequest = createRetrieveRTPRequest();
        retrieveRTPRequest.getRequestInfo().setApTransactionId(null);
        retrieveRTPRequest.getRequestInfo().setApTRId(null);
        retrieveRTPRequest.getRequestInfo().setBRN(responseRTPType.getResponseInfo().getBRN());
    }

    @Given("the BANKID in the request will be validated against the BANKID reference data")
    public void givenValidBankIdInEcomRequest() {
        retrieveRTPRequest.getRequestInfo().setFIId("000002");

    }


    @Given("the COSUMERID in the request will be validated against the CONSUMERID reference data")
    public void givenValidConsumerIdInEcomRequest() {
        retrieveRTPRequest.getRequestInfo().setFIConsumerId("hsbccon2");
    }

    @Given("the request has the BRN")
    public void givenRequestwithValidBRN() {
        retrieveRTPRequest.getRequestInfo().setBRN(responseRTPType.getResponseInfo().getBRN());
    }

    @Given("the expiry timestamp of the request to pay record related to te BRN is within a valid expiry time")
    public void givenWithinLimitExpiryTime() {

    }

    @When("APCore should send an respone to service consumer")
    public void whenApCoreSendsResponse() throws RetrieveRTP1Fault {
        final ManagePayment1PortType managePayment1PortType = factory.getBean(ManagePayment1PortType.class);
        JaxbUtil.newInstance().javaToXML(RetrieveRTP1Request.class, retrieveRTPRequest);
        RetrieveRTP1Response retrieveRTPResponse = managePayment1PortType.retrieveRTP1(retrieveRTPRequest);
        LOG.info("When -  Response Received");
        JaxbUtil.newInstance().javaToXML(RetrieveRTP1Response.class, retrieveRTPResponse);

        if (retrieveRTPResponse != null) {
            if (retrieveRTPResponse.getRTPSummary() != null) {
                storyContext.put(RTP_SUMMARY, retrieveRTPResponse.getRTPSummary());
            }
            if (retrieveRTPResponse.getRTPSenderInfo() != null) {
                storyContext.put(RTP_SENDER_INFO, retrieveRTPResponse.getRTPSenderInfo());
            }
            if (retrieveRTPResponse.getMsgHdr() != null) {
                storyContext.put(RTP_MSGTYPE, retrieveRTPResponse.getMsgHdr().getMsgType().toString());
            }
        }
    }


    @Then("the record related to te BRN should be updated with the status RTP_RETRIEVED")
    public void thenPaymentOrderUpadtedRETRIEVEDforEcom() {

    }

    @Then("the RTPFORAUTH message is sent to the service consumer")
    public void thenRTPFORAUTHSenttoServiceConsumer() {
        doRetrieveRTPResponseAssertions(Nullable.nullable(storyContext.get(RTP_SUMMARY)), Nullable.nullable(storyContext.get(RTP_SENDER_INFO)), Nullable.nullable(storyContext.get(RTP_MSGTYPE)));
    }


}
